package com.parenting.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author zcj
 * @description
 * @date 2021/05/2021/5/31
 **/
public class IMUtil {

    public static JSONObject senMsg(String url, String account, String context) throws JsonProcessingException {
        Map<String, Object> sendIm = new LinkedHashMap<>();
        sendIm.put("SyncOtherMachine", 2);
        sendIm.put("To_Account", account);
        sendIm.put("MsgRandom", RandomUtil.randomInt(0, 2147483647));
        sendIm.put("MsgTimeStamp", System.currentTimeMillis() / 1000L);
        LinkedList<Map<String, Object>> MsgBodyList = new LinkedList<>();
        Map<String, Object> MsgBodyMap = new LinkedHashMap<>();
        MsgBodyMap.put("MsgType", "TIMTextElem");
        Map<String, Object> countMap = new LinkedHashMap<>();
        countMap.put("Text", context);
        MsgBodyMap.put("MsgContent", countMap);
        MsgBodyList.add(MsgBodyMap);
        sendIm.put("MsgBody", MsgBodyList);
        ObjectMapper objectMapper = new ObjectMapper();
        String result = HttpRequest.post(url).body(objectMapper.writeValueAsString(sendIm)).execute().body();
        return JSONUtil.parseObj(result);
    }
}
